package model

import (
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

// user
type UserModel struct {
	gorm.Model
	Username string
	Password string
	Email    string
	Phone    string
	IsDelete int
}


//token
type UserTokenModel struct {
	UserId int
	Token  string
}

//blog
type BlogModel struct {
	gorm.Model
	Title   string
	Comment string `gorm:"type:text"`
	Desc    string
	UserId  int
	IsDelete int `gorm:"type:text"`
}

//comment
type BlogCommentModel struct {
	gorm.Model
	Comment string
	Star    int8
	BlogId  int
	UserId  int
	IsDelete int
}

func MigRate() {
	db := GetDB()
	//db.CreateTable(&UserModel{}, &UserTokenModel{}, &BlogModel{}, &BlogCommentModel{}) //创建表
	db.AutoMigrate(&UserModel{}, &UserTokenModel{}, &BlogModel{}, &BlogCommentModel{}) // students.StudentModel{})	//迁移

}
func GetDB()(*gorm.DB){
	db, err := gorm.Open("mysql", "root:root@tcp(127.0.0.1:3306)/gin_blog?charset=utf8&parseTime=True&loc=Local")
	db.LogMode(true)	//打印sql语句
	if err!=nil{
		fmt.Println("数据库连接成功")
	}
	return db
}
